package com.winit.openapi.util;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.winit.common.query.Searchable;
import com.winit.common.query.Sort;
import com.winit.common.query.Sort.Direction;
import com.winit.common.spi.command.SPICommand;
import com.winit.common.spi.context.CommandContext;


public class CtxUtil {
    
    public static void setCommandCtx(SPICommand command){
        command.setCtx(CommandContext.getContext());
    }
    
    /**
     * 获取默认的语言
     * TODO Add comments here.
     * @return
     */
    public static Locale getLocale(){
    	return CommandContext.getContext().getLocale();
    }
    
    public static Searchable getSearchable(HttpServletRequest request){
        return getSearchable(request, null, null);
    }
    
    public static Searchable getSearchable(HttpServletRequest request,Map<String,String> columnsMapping,String defaultSortField)
    {
        Searchable searchable= Searchable.newSearchable();
        
        int displayStart            = getIntParms("iDisplayStart",0,request);// 起始位置
        int displayLength           = getIntParms("iDisplayLength",10,request); // 每页显示条数
        String field                = request.getParameter("iSortCol_0");// 排序字段
        String ispOrderDirection    = request.getParameter("sSortDir_0");// 排序方式
       
        
        Direction direction = Direction.DESC;
        if ("asc".equalsIgnoreCase(ispOrderDirection)) {
            direction = Direction.ASC;
        }
        
        Integer pageNo= displayStart / displayLength;
        searchable.setPage(pageNo, displayLength);
       
        if (StringUtils.isNotEmpty(field) && columnsMapping != null && columnsMapping.get(field) != null) {
            searchable.addSort(new Sort(direction, columnsMapping.get(field)));
        } else if(StringUtils.isNotEmpty(defaultSortField)){
            searchable.addSort(new Sort(Direction.DESC, defaultSortField));
        }
         
        return searchable;
    }
    
    public static int getIntParms(String name,HttpServletRequest request){
           return getIntParms(name, 0,request);
    }
    
    public static int getIntParms(String name,int defaultVal,HttpServletRequest request){
        String val =  request.getParameter(name);
        try{
           return Integer.parseInt(val);
        }catch (NumberFormatException e) {
            return defaultVal;
        }
     }
    
    public static String getStringParms(String name,HttpServletRequest request){
        return getStringParms(name,"",request);
    }
    
    public static String getStringParms(String name,String defaultVal,HttpServletRequest request){
        String val =  request.getParameter(name);
        if(val == null || val.length() == 0){
            return defaultVal;
        }
        return val;
    }
    
    
    /**
     * 仅保留日期，其它的清0， 如2010-7-27 00:00:00.000
     * 
     * @param date
     * @return
     */
    public static Date getRealDate(Date date) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(date.getTime());
        gc.set(GregorianCalendar.HOUR_OF_DAY, 0);
        gc.set(GregorianCalendar.MINUTE, 0);
        gc.set(GregorianCalendar.SECOND, 0);
        gc.set(GregorianCalendar.MILLISECOND, 0);
        return new Date(gc.getTimeInMillis());
    }

    /**
     * 获取日期所在当前的最大值 ，如2010-7-27 23:59:59.000
     * 
     * @param date
     * @return
     */
    public static Date getFullDate(Date date) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(date.getTime());
        gc.set(GregorianCalendar.HOUR_OF_DAY, 23);
        gc.set(GregorianCalendar.MINUTE, 59);
        gc.set(GregorianCalendar.SECOND, 59);
        gc.set(GregorianCalendar.MILLISECOND, 0);
        return new Date(gc.getTimeInMillis());
    }

}
